﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AllInOne.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System.Configuration;

namespace AllInOne
{
    public class ServiceApi
    {

        public static string BASE_URL = "http://192.168.2.100:8082";
        public static string USER_NAME = "";
        public static void InitProperties()
        {
            AppSettingsReader reader = new AppSettingsReader();
            BASE_URL = (string)reader.GetValue("BASE_URL", typeof(string));
            USER_NAME = (string)reader.GetValue("USER_NAME", typeof(string));

        }


        public static DrivingLicence GetdrivingLicenseByIdNumber(string idNumber)
        {
            string url = BASE_URL + "/cjg/drivingLicence";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.AddParameter("idNumber", idNumber);
            var response = client.Execute(request);
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            DrivingLicence drivingLicense = DrivingLicence.ParseObject(json);
            return drivingLicense;
        }

        public static CarInfo GetCarInfo(string carNumber)
        {
            string url = BASE_URL + "/cjg/carInfo";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.AddParameter("carNumber", carNumber);
            var response = client.Execute(request);
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            CarInfo carInfo = CarInfo.ParseObject(json);
            return carInfo;
        }

        public static JObject hqzp(string sfzmhm)
        {
            string url = BASE_URL + "/cjg/hqzp";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.AddParameter("sfzmhm", sfzmhm);
            var response = client.Execute(request);
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            return json;
        }



        public static long UploadTp(string sfzmhm,string xm, string tp,string type)
        {
            string url = BASE_URL + "/upload/"+type;
            var client = new RestClient(url);
            var request = new RestRequest(Method.POST);
            JObject inputJson = new JObject();
            inputJson["sfzmhm"] = sfzmhm;
            inputJson["xm"] = xm;
            inputJson["tp"] = tp;

            request.AddParameter("application/json", inputJson.ToString(), ParameterType.RequestBody);
            
            var response = client.Execute(request);
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            long sfzId =  (long)json["id"];
            return sfzId;
        }

        public static JObject bzhz(BusinessInfo businessInfo,DrivingLicence drivingLicense)
        {
            SimpleConsole.WriteInfo("开始提交", new ServiceApi());
            string url = BASE_URL + "/cjg/bzhz";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.AddParameter("ywlx", (int)businessInfo.businessType);
            request.AddParameter("sfzmhm", drivingLicense.sfzmhm);
            request.AddParameter("xm", drivingLicense.xm);
            request.AddParameter("dabh", drivingLicense.dabh);
            request.AddParameter("xb", drivingLicense.xb);
            request.AddParameter("csrq", drivingLicense.csrq);
            request.AddParameter("yjdz", drivingLicense.lxzsxxdz);
            request.AddParameter("lxzsxzqh", drivingLicense.lxzsxzqh);
            request.AddParameter("gddh", drivingLicense.lxdh);
            request.AddParameter("dzxx", drivingLicense.dzyx);
            request.AddParameter("sjhm", drivingLicense.sjhm);
            request.AddParameter("xsjhm",businessInfo.sjhm==null?"":businessInfo.sjhm);
            request.AddParameter("yzbm", drivingLicense.lxzsyzbm);
            request.AddParameter("qmId", businessInfo.qmId);
            request.AddParameter("xczpId", businessInfo.xczpId);
            request.AddParameter("sfzId", businessInfo.sfzId);
            request.AddParameter("yhdh", USER_NAME);
            request.AddParameter("zpId", businessInfo.zpId);
            var response = client.Execute(request);
            SimpleConsole.WriteInfo(response.Content, new ServiceApi());
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            return json;
        }

        public static JObject cxlsxx(string lsh)
        {
            string url = BASE_URL + "/cjg/cxlsxx";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.AddParameter("lsh", lsh);
            var response = client.Execute(request);
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            return json;
        }

        public static JObject lnmj(BusinessInfo businessInfo, CarInfo carInfo,string glbm)
        {
            string url = BASE_URL + "/cjg/lnmj";
            var client = new RestClient(url);
            var request = new RestRequest();
            request.AddParameter("glbm", glbm);
            SimpleConsole.WriteInfo("hpzl"+carInfo.hpzl,new ServiceApi());
            request.AddParameter("hpzl", carInfo.hpzl);
            request.AddParameter("fzjg", carInfo.fzjg);
            request.AddParameter("hphm", carInfo.hphm);
            request.AddParameter("xh", carInfo.xh);
            request.AddParameter("zzrq", carInfo.bxzzrq);
            request.AddParameter("sfzId", businessInfo.sfzId);
            request.AddParameter("qmId", businessInfo.qmId);
            request.AddParameter("xm", carInfo.syr);
            request.AddParameter("sfzmhm", carInfo.sfzmhm);
            request.AddParameter("sjhm", carInfo.sjhm);
            request.AddParameter("gddh", carInfo.lxdh);
            request.AddParameter("yjdz", carInfo.zsxxdz);

            var response = client.Execute(request);
            JObject json = (JObject)JsonConvert.DeserializeObject(response.Content);
            return json;
        }



    }
}
